package com.aiyige.page.player.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.aiyige.R;
import com.aiyige.base.BaseActivity;
import com.aiyige.page.player.PlaySpeed;
import com.aiyige.page.player.PlayerController;
import com.aiyige.utils.UIHelper;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiachao on 2017/11/9.
 */

public class DialogPlaySpeed extends PopupWindow {
    Context mContext;
    View mView;
    RecyclerView recyclerView;
    PlaySpeedSelectionAdapter adapter;
    PlayerController playerController;

    public DialogPlaySpeed(Context context, PlayerController playerController) {
//        super(context);
        this.mContext = context;
        this.playerController = playerController;
        init();
    }

    private void init() {
        setTranslucentStatus();
        fitPopupWindowOverStatusBar(true);
        this.mView = LayoutInflater.from(mContext).inflate(R.layout.dialog_player_menu, null);
//        ((BaseActivity)mContext).getWindow().setContentView(mView);
        setContentView(mView);
        recyclerView = (RecyclerView)mView.findViewById(R.id.rv_player_menu);
        this.setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
        this.setWidth(UIHelper.dip2px(150));

        // 设置弹出窗体可点击
        this.setFocusable(true);
//         实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
//        // 设置弹出窗体的背景
        this.setBackgroundDrawable(dw);

        // 设置弹出窗体显示时的动画，从底部向上弹出
        this.setAnimationStyle(R.style.player_selection_dialog_anim);
        this.setOutsideTouchable(true);
        adapter = new PlaySpeedSelectionAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(adapter);
//        recyclerView.addItemDecoration(new MyRecyclerViewDivider(
//                this, LinearLayoutManager.VERTICAL, R.color.white));
        adapter.setCurrentPlaySpeed(playerController.getPlaySpeed());
        adapter.addData(getSpeedList());
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter ad, View view, int position) {
                playerController.setPlaySpeed((PlaySpeed) ad.getData().get(position));
                adapter.setCurrentPlaySpeed((PlaySpeed) ad.getData().get(position));
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void setTranslucentStatus() {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0 全透明实现
            Window window = ((BaseActivity)mContext).getWindow();
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else {//4.4 全透明状态栏
            ((BaseActivity)mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    public void fitPopupWindowOverStatusBar(boolean needFullScreen) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            try {
                Field mLayoutInScreen = PopupWindow.class.getDeclaredField("mLayoutInScreen");
                mLayoutInScreen.setAccessible(true);
                mLayoutInScreen.set(this, needFullScreen);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }


    public void notifyDataChanged() {
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    private List<PlaySpeed> getSpeedList() {
        List<PlaySpeed> list = new ArrayList<>();
        list.add(PlaySpeed.newBuilder().name("20%").value(PlaySpeed.SPEED_PERCENT_20).build());
        list.add(PlaySpeed.newBuilder().name("40%").value(PlaySpeed.SPEED_PERCENT_40).build());
        list.add(PlaySpeed.newBuilder().name("60%").value(PlaySpeed.SPEED_PERCENT_60).build());
        list.add(PlaySpeed.newBuilder().name("80%").value(PlaySpeed.SPEED_PERCENT_80).build());
        list.add(PlaySpeed.newBuilder().name("100%").value(PlaySpeed.SPEED_PERCENT_100).build());
        list.add(PlaySpeed.newBuilder().name("150%").value(PlaySpeed.SPEED_PERCENT_150).build());
        list.add(PlaySpeed.newBuilder().name("200%").value(PlaySpeed.SPEED_PERCENT_200).build());
       return list;
    }

    public void show() {
        int height = playerController.getLayoutParams().height;
        showAtLocation(playerController, Gravity.RIGHT,0,height);
//        show(((BaseActivity)mContext).getSupportFragmentManager(),"");
    }

    @Override
    public void dismiss() {
        super.dismiss();
        playerController.showWidget();
        UIHelper.hideSystemUI((BaseActivity)mContext);
    }
}
